<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/17
 * Time: 14:15
 */

namespace addons\seller\controller;


use app\common\Crypt;
use app\common\model\Sellers;
use app\common\model\Users;
use app\common\Session;

class User extends Common
{
    /**
     * 商家进行登录
     * @return mixed
     */
    public function login()
    {
        $param = input();
        if (isset($param['name']) && isset($param['pwd']))
        {
            if( empty($param['name']) || empty($param['pwd']))  #参数判断
            {
                $this->error('请输入账号和密码');
            }
            if ($user = Users::get_user($param['name']))    #是否存在此账户
            {
                if ($user->password == Crypt::pwd($param['pwd']))  #验证密码
                {
                    if($seller = Sellers::get_seller_by_user_id($user->id)) #验证是否存在此商家
                    {
                        Session::login_success($user);
                        $this->error('前往主页', addon_url('seller://index/index'));
                    }else
                    {
                        $this->error('您还不是商家,无此权限');
                    }
                }else
                {
                    $this->error('密码错误');
                }
            }else
            {
                $this->error('不存在此账号');
            }

        }
        return $this->fetch();
    }

    /**
     * 退出
     */
    public function logout()
    {
        Session::logout();
        $this->success("退出成功", addon_url('seller://user/login'));
    }


    public function user_edit()
    {
        $this->check_login();
        return $this->fetch();
    }


}